<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-input
            v-model="searchModel.descName"
            placeholder="中文描述"
            clearable
          />
          <el-button
            type="primary"
            round
            icon="el-icon-search"
            @click="getCrawlerConfigList"
          >查询
          </el-button>
        </el-col>
        <el-col :span="4" align="right">
          <el-button
            type="primary"
            icon="el-icon-plus"
            circle
            @click="openEditUI(null)"
          />
        </el-col>
      </el-row>
    </el-card>

    <!-- 结果列表 -->
    <el-card>
      <el-table :data="crawlerConfigList" stripe style="width: 100%">
        <el-table-column label="#" width="80">
          <template slot-scope="scope">
            <!-- (pageNo-1) * pageSize + index + 1 -->
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="路由" width="180"/>
        <el-table-column prop="descName" label="中文描述" width="180"/>
        <el-table-column prop="url" label="url" width="480">
          <template slot-scope="scope">
            <a :href="scope.row.url" target="_blank" class="blue-link">{{ scope.row.url }}</a>
          </template>
        </el-table-column>
        <el-table-column prop="size" label="默认采集页数" width="180"/>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="openEditUI(scope.row.id)"/>
            <el-button type="danger" icon="el-icon-delete" size="mini" circle @click="deleteCrawlerConfig(scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 用户信息编辑对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      @close="clearForm"
    >
      <el-form ref="userFormRef" :model="userForm">
        <el-form-item
          label="路由"
          prop="name"
          :label-width="formLabelWidth"
        >
          <el-input v-model="userForm.name" autocomplete="off"/>
        </el-form-item>

        <el-form-item
          label="中文描述"
          prop="descName"
          :label-width="formLabelWidth"
        >
          <el-input v-model="userForm.descName" autocomplete="off"/>
        </el-form-item>

        <el-form-item
          label="url"
          prop="url"
          :label-width="formLabelWidth"
        >
          <el-input v-model="userForm.url" autocomplete="off"/>
        </el-form-item>

        <el-form-item
          label="size"
          prop="size"
          :label-width="formLabelWidth"
        >
          <el-input v-model="userForm.size" autocomplete="off"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveCrawlerConfig">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import userApi from '@/api/userManage'

export default {
  data() {
    return {
      formLabelWidth: '130px',
      userForm: {},
      dialogFormVisible: false,
      title: '',
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 10,
        descName: ''
      },
      crawlerConfigList: []
    }
  },
  created() {
    this.getCrawlerConfigList()
  },
  methods: {
    deleteCrawlerConfig(config) {
      this.$confirm(`您确认删除用户 ${config.descName} ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        userApi.deleteCrawlerConfig(config.id).then(response => {
          this.$message({
            type: 'success',
            message: response.message
          })
          this.getCrawlerConfigList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    saveCrawlerConfig() {
      // 触发表单验证
      this.$refs.userFormRef.validate((valid) => {
        if (valid) {
          // 提交请求给后台
          userApi.saveCrawlerConfig(this.userForm).then(response => {
            // 成功提示
            this.$message({
              message: response.message,
              type: 'success'
            })
            // 关闭对话框
            this.dialogFormVisible = false
            // 刷新表格
            this.getCrawlerConfigList()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    clearForm() {
      this.userForm = {}
      this.$refs.userFormRef.clearValidate()
    },
    openEditUI(id) {
      if (id == null) {
        this.title = '新增用户'
      } else {
        this.title = '修改用户'
        // 根据id查询用户数据
        userApi.getCrawlerConfigById(id).then(response => {
          this.userForm = response.data
        })
      }
      this.dialogFormVisible = true
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize
      this.getCrawlerConfigList()
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo
      this.getCrawlerConfigList()
    },
    getCrawlerConfigList() {
      userApi.getCrawlerConfigList(this.searchModel).then((response) => {
        this.crawlerConfigList = response.data.rows
        this.total = response.data.total
      })
    }
  }
}
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}

.el-dialog .el-input {
  width: 85%;
}
</style>
